<?php
function loadFixtures(& $season, & $teams) {
    $fixtures = array ();
    $rs = executeQuery("select id, round, fk_home_team, fk_away_team from fixtures where fk_season=$season->id order by 2, 1");
    while ($row = mysql_fetch_row($rs)) {
        array_push($fixtures, new Fixture($row[0], $season, $row[1], $teams->getTeam($row[2]), $teams->getTeam($row[3])));
    }

    return $fixtures;

}

function hasFixtures($season) {
    $query = "select count(1) from fixtures where fk_season=$season->id";
    $rs = executeQuery($query);
    $row = mysql_fetch_row($rs);
    return $row[0];
}

function hasFixturesTeam(& $team) {
    $query = "select count(1) from fixtures where fk_home_team=$team->id or fk_away_team=$team->id";
    $rs = executeQuery($query);
    $row = mysql_fetch_row($rs);
    return $row[0];
}

function loadFixturesTeam(& $team) {
	global $teams;
    $fixtures = array ();
    $rs = executeQuery("select id, fk_season, round, fk_home_team, fk_away_team from fixtures " .
    		"where fk_home_team=$team->id or fk_away_team=$team->id " .
    		"order by 2, 3, 1");
    while ($row = mysql_fetch_row($rs)) {
    	$season = new Season($row[1]);
        array_push($fixtures, new Fixture($row[0], $season, $row[2], $teams->getTeam($row[3]), $teams->getTeam($row[4])));
    }

    return $fixtures;

}

function addFixture(& $season, $round, $homeId, $awayId) {
    $query = "insert into fixtures (fk_season, round, fk_home_team, fk_away_team) values ($season->id, $round, $homeId, $awayId)";
    $rs = executeQuery($query);
    return $rs;
}

function deleteFixture($id) {
    $query = "delete from fixtures where id = $id";
    $rs = executeQuery($query);
    return $rs;
}

class Fixture {

    var $id;
    var $season;
    var $round;
    var $homeTeam;
    var $awayTeam;

    function Fixture($id, $season, $round, & $home, & $away) {
        $this->id = $id;
        $this->season = $season;
        $this->round = $round;
        $this->homeTeam = $home;
        $this->awayTeam = $away;
    }
}
?>